<template>
  <div class="BankSummary">
    <div class="header">
      <h2>银行日记收支汇总表</h2>
      <div>
        <div>
          <span>查询日期：</span>
          <el-date-picker
            v-model="searchData.settleDate"
            type="month"
            value-format="yyyyMM"
            placeholder="选择月"
          >
          </el-date-picker>
        </div>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="add">新增配置</el-button>
        <el-button type="primary" @click="download">下载模板</el-button>
        <el-upload
          class="upload"
          ref="upload"
          :action="''"
          :on-change="handleChange"
          :auto-upload="false"
          :show-file-list="false"
        >
          <el-button type="primary">导入</el-button>
        </el-upload>
      </div>
      <hr />
    </div>
    <div class="content">
      <el-table
        max-height="620"
        border
        :data="tableData && tableData.list && tableData.list.list"
        tooltip-effect="dark"
        v-loading="loading"
      >
        <el-table-column
          v-for="(t, n) in tableData && tableData.fields"
          :key="n"
          :prop="t.fields"
          :label="t.translate"
          align="center"
          :min-width="
            flexColumnWidth(t.translate, t.fields, tableData.list.list)
          "
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button size="small" type="primary" @click="edit(scope.row)"
              >编辑</el-button
            >
            <!-- <el-button size="small" type="danger" @click="remove(scope.row)"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <pagination
        :current-page="searchData.pageDate.pageNum"
        :page-size="searchData.pageDate.pageSize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div v-if="maskFlag" class="mask">
      <div class="table">
        <div class="flex">
          <div class="title">
            {{ editFlag ? "修改收支配置数据" : "新增收支配置数据" }}
          </div>
          <i class="el-icon-close" @click="close"></i>
        </div>
        <div class="custom">
          <el-form
            label-position="left"
            label-width="150px"
            ref="ruleForm"
            :rules="rules"
            :model="formData"
          >
            <el-form-item label="一级会计科目" prop="firstLevelAccount">
              <el-input v-model="formData.firstLevelAccount"></el-input>
            </el-form-item>
            <el-form-item label="二级科目" prop="secondLevelAccount">
              <el-input v-model.number="formData.secondLevelAccount"></el-input>
            </el-form-item>
          </el-form>
          <div class="btn">
            <el-button type="primary" round @click="save">保存</el-button>
            <el-button round @click="close">取消</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  exportBankSummary,
  getBankSummary,
  addBankSummary,
  editBankSummary,
  removeBankSummary,
  importBankSummary,
} from "@/api/statisticalCenter";

export default {
  name: "BankSummary",
  data() {
    return {
      searchData: {
        settleDate: "",
        pageDate: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      total: 0,
      tableData: [],
      loading: false,
      maskFlag: false,
      editFlag: false,
      formData: {
        firstLevelAccount: "",
        secondLevelAccount: "",
      },
      rules: {
        firstLevelAccount: [{ required: true, message: "必填项" }],
        secondLevelAccount: [{ required: true, message: "必填项" }],
      },
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    search() {
      if (this.searchData.settleDate) {
        this.getTableData();
      } else {
        this.$message.warning("请选择查询月份");
      }
    },
    handleChange(file, fileList) {
      const fileType = [
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "application/vnd.ms-excel",
      ];
      if (fileType.includes(file.raw.type)) {
        const formData = new FormData();
        formData.append("file", file.raw);
        importBankSummary(formData).then((res) => {
          if (res.code == 200) {
            this.$message.success("导入成功");
            this.getTableData();
          }
        });
      } else {
        this.$notify.error("只能上传Excel文件！");
      }
    },
    remove(item) {
      removeBankSummary(item.id).then((res) => {
        if (res.code == 200) this.$message.success("删除成功");
      });
    },
    edit(item) {
      this.editFlag = true;
      this.formData = { ...item };
      this.formData.id = this.formData.keywordId;
      this.maskFlag = true;
    },
    save() {
      let url = this.editFlag ? editBankSummary : addBankSummary;
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          url(this.formData).then((res) => {
            if (res.code == 200) {
              this.$message.success(this.editFlag ? "编辑成功" : "添加成功");
              this.maskFlag = false;
              this.getTableData();
            }
          });
        } else {
          return false;
        }
      });
    },
    add() {
      this.editFlag = false;
      this.maskFlag = true;
    },
    close() {
      this.maskFlag = false;
      this.formData = {};
    },
    getTableData() {
      this.loading = true;
      getBankSummary(this.searchData).then((res) => {
        this.tableData = res.data;
        this.total = res.data.list.total;
        this.loading = false;
      });
    },
    handleSizeChange(size) {
      this.searchData.pageDate.pageSize = size;
      this.getTableData();
    },
    handleCurrentChange(current) {
      this.searchData.pageDate.pageNum = current;
      this.getTableData();
    },
    download() {
      this.$notify.info({
        title: "开始准备下载...",
      });
      exportBankSummary().then((res) => {
        const url = window.URL.createObjectURL(new Blob([res]), {
          type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        });
        const link = document.createElement("a");
        link.href = url;
        link.setAttribute("download", "收支汇总表.xlsx");
        document.body.appendChild(link);
        link.click();
        link.parentNode.removeChild(link);
        this.$notify.success("下载成功!");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.BankSummary {
  padding: 20px;
}
.header {
  h2 {
    text-align: center;
    color: skyblue;
  }
  & > div {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    & > div {
      margin-right: 20px;
    }
  }
  .el-button {
    margin: 0 20px 0 0;
  }
  hr {
    margin: 16px 0;
    color: skyblue;
  }
}
.footer {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  background: rgba($color: #000000, $alpha: 0.2);
  .flex {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
  }
  .title {
    padding-left: 12px;
    border-left: 6px solid skyblue;
    font-size: 24px;
    font-weight: bold;
  }
  i {
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
  }
  .table {
    margin: 80px auto;
    padding: 30px;
    width: 30%;
    border-radius: 24px;
    background-color: #fff;
    .custom {
      margin-bottom: 20px;
      padding: 0 20px;
      .btn {
        display: flex;
        justify-content: space-around;
        .el-button {
          padding: 12px 36px;
          font-size: 16px;
        }
      }
    }
  }
  &::v-deep .t-table {
    .t-table__header {
      tr th {
        font-size: 16px;
        font-weight: bolder;
      }
    }
    .t-table__body {
      tr td {
        font-size: 16px;
        font-weight: bolder;
        color: #666;
      }
    }
  }
}
</style>
